package asteroids.entity.weapon;

import java.util.ArrayList;

import asteroids.engine.GameState;
import asteroids.engine.Utils;
import asteroids.entity.PlayerShip;
import asteroids.entity.Ship;

/**
 * Fires single bullets forward, rotating between the ship's hardpoints.
 * 
 * @author whitecd1
 * 
 */
public class ClassicWeapon extends Weapon
{
    double bulletSpeed = 240.0 / Utils.FRAMES_PER_SECOND;
    int bulletTimeToLive = (int) (1.5 * Utils.FRAMES_PER_SECOND);

    int maxBullets = 3;
    // final int REFIRE_DELAY;
    long lastTimeFired = 0;
    // separate ArrayList for keeping track of bullets generated by this gun.
    // The ship may be handling bullets that
    // originated elsewhere, so we want to be sure to only destroy/modify
    // bullets from this gun.
    ArrayList<Bullet> bullets;

    int currentHardPoint = 0;

    public ClassicWeapon(Ship ship, int cooldown)
    {
        super(
                ship,
                0,
                (int) ((Utils.FRAMES_PER_SECOND / 3.0) / ((PlayerShip) ship).getHardPoints().length));

        // REFIRE_DELAY = (int) ((Utils.FRAMES_PER_SECOND / 3.0) / ((PlayerShip)
        // ship).getHardPoints().length);

        // (int) (0.33 * Utils.MILLISECONDS_PER_SECOND / ((PlayerShip) ship)
        // .getHardPoints().length);

        bullets = new ArrayList<Bullet>();
    }

    public ClassicWeapon(Ship ship)
    {
        this(ship, (int) (0.33 * Utils.FRAMES_PER_SECOND));
    }

    /**
     * returns true if the weapon discharges, false else
     */
    public boolean FIRE(GameState state)
    {
        boolean fired = false;
        if( canFireYet(state) )
        {
            manageFireControl(state);
            fired = true;
            Utils.PolarCoordinate[] hardPoints = ((PlayerShip) ship).getHardPoints();
            double rotation = ship.getRotation();

            if( maxBullets * hardPoints.length <= bullets.size() )
            {
                bullets.get(0).destroy(); // kill the oldest one
                bullets.remove(0); // remove it here; the ship will also remove
                                   // it on next update
            }

            Utils.CartesianCoordinate pos = Utils.cartesianFromPolar(
                    hardPoints[currentHardPoint].rotateBy(rotation)).shiftBy(ship.getXPosition(),
                    ship.getYPosition());
            Bullet bill = new Bullet(pos.getXComponent(), pos.getYComponent(), rotation,
                    bulletSpeed, 1, bulletTimeToLive, ship);
            bill.addVelocity(Utils.cartesianFromPolar(Utils.createPolarCoordinate(ship.direction,
                    ship.speed)));
            bill.setAffiliation(ship.getAffiliation());
            ship.add(bill);
            bullets.add(bill);
            currentHardPoint = (currentHardPoint + 1) % hardPoints.length;
            lastTimeFired = state.currentGameTime();
        }
        return fired;
    }
}
